/*
 * Copyright (c) YouYuan 2018. https://gitee.com/you-yuan
 */

package com.yuan.crawler.zhidao.core;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.yuan.crawler.common.HtmlUnitCommon;
import com.yuan.crawler.proxy.ProxyServer;
import com.yuan.crawler.proxy.impl.ZMProxyServerImpl;
import com.yuan.crawler.zhidao.bean.ZhiDaoInfo;
import com.yuan.util.collection.ListUtil;
import com.yuan.util.string.StrUtil;

import java.util.List;

/**
 * 百度知道结果爬取
 * @author YouYuan
 * @version 1.0
 * @date 2018/9/7 12:06
 */
public class ZhiDaoCrawler {

    private static final String ZHIDAO_API = "https://zhidao.baidu.com/search?word=";

    private static final ProxyServer PROXY_SERVER = new ZMProxyServerImpl();

    public static void main(String[] args) throws Exception {
        ZhiDaoInfo rs = search("此生读过的最美的诗是");
        System.out.println(rs);
    }

    public static ZhiDaoInfo search(String question) throws Exception {
        String searchUrl = ZHIDAO_API + question;
        WebClient webClient = HtmlUnitCommon.initWebClient();
        HtmlPage page = webClient.getPage(searchUrl);
//        System.out.println(page.asXml());
        List<HtmlElement> resultList = page.getByXPath("//div[@class='list-inner']/*//a[@class='ti'][1]");
        if (ListUtil.notEmpty(resultList)) {
            String answerHref = resultList.get(0).getAttribute("href");
            HtmlPage answerPage = webClient.getPage(answerHref);
//            String s = answerPage.asXml();
//            System.out.println(s);
            List<HtmlElement> answerList = answerPage.getByXPath("//div[@accuse='aContent']");
            if (ListUtil.notEmpty(answerList)) {
                HtmlElement bestAnswer = answerList.get(0);
                String bestAnswerText = bestAnswer.asText();
//                System.out.println(bestAnswerText);
                if (StrUtil.notEmpty(bestAnswerText) && bestAnswerText.startsWith("展开全部")) {
                    bestAnswerText = bestAnswerText.substring(6).trim();
                }
                ZhiDaoInfo zidao = new ZhiDaoInfo();
                zidao.setQuestion(question);
                zidao.setAnswer(bestAnswerText);
                return zidao;
            }
        }
        return null;
    }
}
